﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Entity.EntityDto.Goods;
using VM.Entity.EntityDto.Lunbo;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.Goods;
using VM.IBLL.System.Code;
using VM.IBLL.Topic;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{

    /// <summary>
    /// 商品相关控制器
    /// </summary>
    [Route("api/goods")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly ICodeHelperBLL _ICodeHelperBLL;
        private readonly IGoodsBLL _IGoodsBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;

        public GoodsController(IJWTService iJWTService, ICodeHelperBLL iCodeHelperBLL, IHttpContextAccessor httpContextAccessor, IGoodsBLL iGoodsBLL)
        {

            _ICodeHelperBLL = iCodeHelperBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
            _IGoodsBLL = iGoodsBLL;
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addGoods")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addGoods(addGoodsMainInfoDto obj)
        { 
            //解析Jwt,获取到UserId
            int UserId= _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj= _ICodeHelperBLL.perfectClass<addGoodsMainInfoDto>(obj,UserId,ProTypeEnum.save);
            return _IGoodsBLL.addGoods(obj);
        }

        /// <summary>
        /// 添加商品的规格属性
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addGoodSpec")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addGoodSpec(addGoodSpecDto obj)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj = _ICodeHelperBLL.perfectClass<addGoodSpecDto>(obj, UserId, ProTypeEnum.save);
            return _IGoodsBLL.addGoodSpec(obj);
        }

        /// <summary>
        /// 获取首页商品轮播列表/获取活动页轮播列表，   PHONE
        /// </summary>
        /// <param name="lunBoTypeId">轮播类型  1 首页轮播  2 活动页轮播</param>
        /// <returns></returns>
        [HttpGet]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getGoodsLunBoList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<List<getLunboListDto>> getGoodsLunBoList(int lunBoTypeId)
        {


           //解析Jwt,获取到UserId
            //int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            ApiDataResult<List<getLunboListDto>> Res = _IGoodsBLL.getGoodsLunBoList(lunBoTypeId);
            return Res;
        }


        /// <summary>
        /// 根据商品类别获取商品列表信息，多个地方都能用到
        /// </summary>
        /// <param name="goodsTypeId">类别内码</param>
        /// <returns></returns>
        [HttpGet]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getGoodsListByTypeId")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getGoodsListByTypeIdDto>> getGoodsListByTypeId(int goodsTypeId, int pageIndex=1, int pageSize=100)
        {   
            //解析Jwt,获取到UserId
            //int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            ApiDataResult<PagingData<getGoodsListByTypeIdDto>> Res = _IGoodsBLL.getGoodsListByTypeId(  goodsTypeId,   pageIndex,   pageSize);
            return Res;
        }


        /// <summary>
        /// 获取商品详情，根据商品列表的id内码
        /// </summary>
        /// <param name="goodsId">商品内码</param>
        /// <returns></returns>
        [HttpGet]
        //[Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getGoodsInfoById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<getGoodsInfoByIdDto> getGoodsInfoById(int goodsId)
        {
            //解析Jwt,获取到UserId
            //int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            ApiDataResult<getGoodsInfoByIdDto> Res = _IGoodsBLL.getGoodsInfoById(goodsId);
            return Res;
        }

    }
}
